<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\User;

use App\Dao\GoodsDao;
use App\Dao\GoodsPropertyDao;
use App\Dao\GoodsPropertyValueDao;
use App\Dao\IntegralDetailDao;
use App\Dao\UserBillDao;
use App\Dao\UserDao;
use App\Exception\CustomException;
use App\Kernel\Util\UserJwtInstance;
use App\Service\Service;
use App\Dao\UserAddrDao;
use App\Dao\GoodsSkuDao;
use App\Dao\OrderDao;

use Hyperf\DbConnection\Db;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * 商品服务
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\User
 */
class GoodsService  extends Service
{

    /**
     * @Inject
     *
     * @var GoodsPropertyDao
     */
    private $goods_property;

    /**
     * @Inject
     *
     * @var GoodsPropertyValueDao
     */
    private $goods_property_value;

    /**
     * @Inject
     *
     * @var GoodsSkuDao
     */
    private $goods_sku;

    /**
     * 处理商品SKU
     * @param object $sku
     * @return array
     */
    public function goods_sku( object $sku )
    {
        $goods_sku = [];
        foreach ( $sku as $key => $value ) {
            $goods_sku[$key]['id'] = $value['id'];
            $goods_sku[$key]['goods_id'] = $value['goods_id'];
            $goods_sku[$key]['name'] = $value['name'];
            $goods_sku[$key]['price'] = $value['price'];
            $goods_sku[$key]['picture'] = $value['picture'];
            $goods_sku[$key]['integral'] = $value['integral'];
            $goods_sku[$key]['stock'] = $value['stock']+10000;
            $goods_sku[$key]['properties'] = $this->properties_sku( $value['properties'] );
        }

        return $goods_sku;
    }

    /**
     * 获取SKU 键-值
     *
     * @param array $properties_sku
     * @return array
     */
    public function properties_sku( array $properties_sku )
    {
        $properties = [];
        foreach ( $properties_sku as $key => $value ) {
            $properties[$key] = $this->goods_property_value->first($value);
        }
        $result = [];
        foreach ($properties as $key => $value) {
            $result[$key]['key'] = $this->goods_property->first($value['property_id']);
            $result[$key]['value'] = $value['value'];
        }
        return $result;
    }
}